package com.wk.warehouse.controller;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.*;
import com.wk.warehouse.page.Page;
import com.wk.warehouse.service.*;
import com.wk.warehouse.util.CurrentUser;
import com.wk.warehouse.util.TokenUtils;
import com.wk.warehouse.util.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 调货管理后台
 */
@RestController
@RequestMapping("/transshipment")
public class TransshipmentController {

	@Autowired
	private TransshipmentService transshipmentService;
	@Autowired
	BrandService brandService;
	@Autowired
	InStoreService inStoreService;
	@Autowired
	ProductService productService;
	@Autowired
	private StoreService storeService;

	/**
	 * 所有仓库
	 */
	@GetMapping("/store-list")
	public Result storeList() {
		return Result.ok(storeService.storeList());
	}

	/**
	 * 所有品牌
	 */
	@GetMapping("/brand-list")
	public Result brandList() {
		return Result.ok(brandService.selectAllBrand());
	}

	/**
	 * 分页模糊查询调货列表
	 */
	@GetMapping("/product-page-list")
	public Result productPageList(Product product, Page page){
		// 获取总条数
		int totalNum = productService.selectCount(product);
		page.setTotalNum(totalNum);

		// 查询商品信息
		List<Product> productList = productService.selectProductMess(product, page);
		page.setResultList(productList);
		return Result.ok(page);
	}

	/**
	 * 分页模糊查询所有调货单
	 */
	@GetMapping("/transshipment-page-list")
	public Result transshipmentPageList(Transshipment transshipment, Page page) {
		// 获取总条数
		int totalNum = transshipmentService.transshipmentCount(transshipment);
		page.setTotalNum(totalNum);

		// 查询调货单
		List<Transshipment> transshipmentList = transshipmentService.selectTransshipment(transshipment, page);
		page.setResultList(transshipmentList);
		return Result.ok(page);
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/exportTable")
	public Result exportTable(Transshipment transshipment, Page page){
		page = new Page(9999, 1); // 导出模糊查询所有数据
		// 要导出的数据
		List<Transshipment> transshipmentList = transshipmentService.selectTransshipment(transshipment, page);
		return Result.ok(transshipmentList);
	}

	/**
	 * 商品编码唯一验证
	 */
	@GetMapping("/product-num-check")
	public Result addTransshipment(String productNum){
		int isExist = productService.findProductByNum(productNum);
		return Result.ok(isExist==0);
	}

	/**
	 * 点击添加调货单的确认按钮，进行操作
	 * 1. 验证目标商品编号的唯一性
	 * 2. 向商品表插入一条数据(源商品id查出商品信息，然后插入，此时商品编号，库存为空)
	 * 3. 查询最大的商品id，即为目标商品id
	 * 4. 修改目标商品的仓库id和商品编号
	 * 5. 向调货单表插入一条数据，调货状态为0，未审核
	 */
	@Transactional
	@PostMapping("/transshipment-add")
	public Result addTransshipment(@RequestBody Transshipment transshipment, Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken){
		// 验证商品编号的唯一性
		if(productService.findProductByNum(transshipment.getTargetProductNum())>0) return Result.err(Result.CODE_ERR_BUSINESS, "该商品编号已存在！");
		// 源商品库存
		Integer sourceProductInvent = productService.findProductInvent(transshipment.getSourceProductId());
		// 调货量不能大于库存
		if(sourceProductInvent==null || transshipment.getTransNum()>sourceProductInvent){
			return Result.err(Result.CODE_ERR_BUSINESS, "调货量不能大于库存！");
		}

		// 1. 添加新的商品信息，目标商品
		productService.insertSelectResult(transshipment.getSourceProductId());
		// 2. 获取最新的商品id，即目标商品id
		int targetProductId = productService.getNewProductId();
		// 3. 更新目标商品的仓库id和商品编号
		product.setProductId(targetProductId);
		product.setStoreId(transshipment.getTargetStoreId());
		product.setProductNum(transshipment.getTargetProductNum());
		productService.updateInsertInfo(product);

		// 获取添加人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int createBy = currentUser.getUserId();
		transshipment.setCreateBy(createBy);
		// 设置目标商品id
		transshipment.setTargetProductId(targetProductId);
		// 添加调货单
		transshipmentService.addTransshipment(transshipment);
		return Result.ok("添加调货单成功！");
	}

	/**
	 * 调货单列表页面，点击确认调货
	 * 1. 修改调货状态 0改为1
	 * 2. 修改源商品库存(减去调货量)
	 * 3. 修改目标商品库存(调货量)
	 * 4. 向入库单插入数据(默认已审核)
	 */
	@Transactional
	@PutMapping("/transshipment-confirm")
	public Result addTransshipment(@RequestBody Transshipment transshipment,  @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken){
		// 1. 修改调货状态 0改为1
		transshipmentService.updateAuditState(transshipment.getTransId());
		// 2. 修改源商品库存(减去调货量)
		productService.updateSourceInvent(transshipment.getSourceProductId(), transshipment.getTransNum());
		// 3. 修改目标商品库存(调货量)
		productService.updateTargetInvent(transshipment.getTargetProductId(), transshipment.getTransNum());
		// 4. 向入库单插入数据(默认已审核)
		InStore inStore = new InStore();
		inStore.setStoreId(transshipment.getTargetStoreId());
		inStore.setProductId(transshipment.getTargetProductId());
		inStore.setInNum(transshipment.getTransNum());
		// 获取添加人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int createBy = currentUser.getUserId();
		inStore.setCreateBy(createBy);
		inStoreService.addInStoreForm1(inStore);
		return Result.ok("调货成功！");
	}

}
